/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import core.Decoder;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

/**
 *
 * @author Kvest
 */
public class Utils
{
    public static int byteToInt(byte[] val)
    {
        int result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue *= 2;
            }
        }

        return result;
    }

    public static byte boolToByte(boolean val)//������� �������� �� boolean � byte
    {
            if (val)
            {
                    return 0x01;
            }
            else
            {
                    return 0x00;
            }

    }

    public static byte[] int16ToByte(int val)//������� �������� �� int16 � ������ byte
    {
            byte t;
            int temp;
            byte[] result;
            result = new byte[2];
            Integer i = new Integer(val);
            t = i.byteValue();
            result[0] = t;
            temp = i.intValue();
            temp = temp>>8;
            i = new Integer(temp);
            t = i.byteValue();
            result[1] = t;
            System.gc();
            return result;
    }

    public static String byteToString(byte[] val)//������� �������� �� ������� ���� � ������
    {
        try
        {
            return new String(val, "utf-8");
        }
        catch(Exception ioex)
        {
            return new String(val);
        }
    }

    private static long byteToLong(byte[] val)
    {
        long result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue = bitValue << 1;
            }
        }

        return result;
    }

    public static double byteToDecimal(byte[] val)
    {
            double k,temp;
            String tempStr;
            int exp;
            tempStr = "";
            temp = 0;
            //��������� � �������� ���
            for(int i = 0;i<val.length;i++)
            {
                    String substr;
                    substr = Integer.toBinaryString(val[i]);
                    if (substr.length()>8)
                    {
                            substr = substr.substring(substr.length()-8);
                    }
                    int delta = 8 - substr.length();
                    for (int j=0;j<delta;j++)
                    {
                            substr = '0'+substr;
                    }
                    tempStr =  substr+tempStr;
            }
            k = 1;
            //�������� ����� �����
            for(int i=tempStr.length()-1;i>=32;i--)
            {
                    if (tempStr.charAt(i)=='1')
                    {
                            temp += k;
                    }
                    k *=2;
            }
            //������� �����
            k = 1;
            exp = 0;
            for (int i=15;i>=8;i--)
            {
                    if (tempStr.charAt(i)=='1')
                    {
                            exp += k;
                    }
                    k *=2;
            }
            if (exp>28)
            {
                    exp = 28;
            }
            k = 1;
            for (int i=1;i<=exp;i++)
            {
                    k *= 10;
            }
            temp = temp / k;
            //���������� ����
            if (tempStr.charAt(0)=='1')
            {
                    temp = -temp;
            }
            return temp;
    }

    public static long byteToDate(byte[] val)
    {
            long l;
            l = byteToLong(val);
            //�������� � �������������
            l = l/10000;
            //���� ���������� ���������� ����
            l -= 62135596800000l;
            return l;
    }

    public static boolean isNumber(char c)
    {
            int code = c;
            if ((code>=48)&&(code<=57))
            {
                    return true;
            }
            else
            {
                    return false;
            }
    }

    public static String dateToString(long ticks)
    {
            Date d = new Date(ticks);
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
            calendar.setTime(d);
            String temp = "";
            if (calendar.get(Calendar.DAY_OF_MONTH)<10)
            {
                    temp += "0"+Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
            }
            else
            {
                    temp += Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
            }
            if ((calendar.get(Calendar.MONTH)+1)<10)
            {
                    temp +=".0" + Integer.toString (calendar.get(Calendar.MONTH)+1);
            }
            else
            {
                    temp +="." + Integer.toString (calendar.get(Calendar.MONTH)+1);
            }
            temp +="." + Integer.toString (calendar.get(Calendar.YEAR));
            temp +=" " + Integer.toString (calendar.get(Calendar.HOUR_OF_DAY));
            if (calendar.get(Calendar.MINUTE)<10)
            {
                    temp +=":0" + Integer.toString (calendar.get(Calendar.MINUTE));
            }
            else
            {
                    temp +=":" + Integer.toString (calendar.get(Calendar.MINUTE));
            }
            return temp;
    }

    public static long stringToDate(String value)
    {
        if (!isInvalidDate(value))
        {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date(0));

            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(value.substring(0,2)));
            calendar.set(Calendar.MONTH, Integer.parseInt(value.substring(3,5)));
            calendar.set(Calendar.YEAR, Integer.parseInt(value.substring(6,10)));
            
            return calendar.getTime().getTime();
        }
        else
        {
            return -1;
        }
    }

    public static String dateToDateString(long ticks)
    {
            Date d = new Date(ticks);
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
            calendar.setTime(d);
            String temp = "";
            if (calendar.get(Calendar.DAY_OF_MONTH)<10)
            {
                    temp += "0"+Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
            }
            else
            {
                    temp += Integer.toString (calendar.get(Calendar.DAY_OF_MONTH));
            }
            if ((calendar.get(Calendar.MONTH)+1)<10)
            {
                    temp +=".0" + Integer.toString (calendar.get(Calendar.MONTH)+1);
            }
            else
            {
                    temp +="." + Integer.toString (calendar.get(Calendar.MONTH)+1);
            }
            temp +="." + Integer.toString (calendar.get(Calendar.YEAR));
            return temp;
    }

    public static String dateToTimeString(long ticks)
    {
            Date d = new Date(ticks);
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));//getDefault());
            calendar.setTime(d);
            String temp = "";
            temp +=Integer.toString (calendar.get(Calendar.HOUR_OF_DAY));
            if (calendar.get(Calendar.MINUTE)<10)
            {
                    temp +=":0" + Integer.toString (calendar.get(Calendar.MINUTE));
            }
            else
            {
                    temp +=":" + Integer.toString (calendar.get(Calendar.MINUTE));
            }
            return temp;
    }

    public static byte[] int32ToByte(int val)//������� �������� �� int32 � ������ byte
    {
            byte t;
            int temp;
            byte[] result;
            result = new byte[4];
            Integer i = new Integer(val);
            t = i.byteValue();
            result[0] = t;
            temp = i.intValue();
            temp = temp>>8;
            i = new Integer(temp);
            t = i.byteValue();
            result[1] = t;
            temp = i.intValue();
            temp = temp>>8;
            i = new Integer(temp);
            t = i.byteValue();
            result[2] = t;
            temp = i.intValue();
            temp = temp>>8;
            i = new Integer(temp);
            t = i.byteValue();
            result[3] = t;
            System.gc();
            return result;
    }

    public static byte[] longToByte(long val)//������� �������� �� long � ������ byte
    {
            byte[] result,temp;
            result = new byte[8];
            int intVal;

            intVal = (int)val;
            temp = int32ToByte(intVal);
            for (int i=0;i<4;i++)
            {
                    result[i] = temp[i];
            }

            val = val>>32;
            intVal = (int)val;
            temp = int32ToByte(intVal);
            for (int i=0;i<4;i++)
            {
                    result[i+4] = temp[i];
            }

            return result;
    }

    public static int getStringSize(String value)
    {
        try
        {
            return (value.getBytes("utf-8").length + 4);
        }
        catch(Exception ioex)
        {
            return (value.getBytes().length + 4);
        }
    }

    public static byte[] stringToByteUTF(String value) throws IOException
    {
        return value.getBytes("utf-8");
    }

    public static byte[] getStringByte(String value)
    {
            byte[] temp,result,size;
            int index;
            try
            {
                    temp = stringToByteUTF(value);
            }
            catch(Exception e)
            {
                    temp = value.getBytes();
            }
            size = int32ToByte(temp.length);
            result = new byte[temp.length+4];
            index = 0;
            //���������� ������ ������
            for (int i=0;i<size.length;i++)
            {
                    result[index++] = size[i];
            }
            //���������� ������
            for (int i=0;i<temp.length;i++)
            {
                    result[index++] = temp[i];
            }
            return result;
    }

    public static String[] split(String src, String delimiter, boolean addEmptyItems)
    {
        StringBuffer token = new StringBuffer();
        Vector tokens = new Vector();
        // split
        char[] chars = src.toCharArray();
        for (int i=0; i < chars.length; i++) 
        {
            if (delimiter.indexOf(chars[i]) != -1) 
            {
                 // we bumbed into a delimiter
                if (token.length() > 0 | addEmptyItems) 
                {
                    tokens.addElement(token.toString());
                    token.setLength(0);
                }
            } 
            else 
            {
                token.append(chars[i]);
            }
        }
        // don't forget the "tail"...
        if (token.length() > 0 | addEmptyItems) 
        {
            tokens.addElement(token.toString());
        }
     
        // convert the vector into an array
        String[] splitArray = new String[tokens.size()];
        for (int i=0; i < splitArray.length; i++) 
        {
            splitArray[i] = (String)tokens.elementAt(i);
        }
        return splitArray;
    }
    
    public static String replace(final String src, final String from, final String to)
    {
        String result = src;
        while (result.indexOf(from) != -1)
        {
            result = result.substring(0, result.indexOf(from)) + to + result.substring(result.indexOf(from) + from.length());
        }

        return result;
    }
    
    public static boolean isInvalidDate(String value)
    {
        value = value.trim();
        return ((value.length() != 10) ||
                (value.charAt(2) != '.') ||
                (value.charAt(5) != '.') ||
                (!Character.isDigit(value.charAt(0))) ||
                (!Character.isDigit(value.charAt(1))) ||
                (!Character.isDigit(value.charAt(3))) ||
                (!Character.isDigit(value.charAt(4))) ||
                (!Character.isDigit(value.charAt(6))) ||
                (!Character.isDigit(value.charAt(7))) ||
                (!Character.isDigit(value.charAt(8))) ||
                (!Character.isDigit(value.charAt(9))));
    }
}
